package touro.finances.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Expense {

	private int expenseID;
	private double amount;
	private int month;
	private int day;
	private int year;
	private String description;
	//private int type;
	
	
	
	/**
	 * Constructor
	 * @param expenseID
	 * @param amount
	 * @param month
	 * @param day
	 * @param year
	 * @param description
	 */
	public Expense(int expenseID, double amount, int month, int day, int year,
			String description) {
		this.expenseID = expenseID;
		this.amount = amount;
		this.month = month;
		this.day = day;
		this.year = year;
		this.description = description;
		//this.type = type;
		
	}
	public Expense(double amount, int month, int day, int year,
			String description) {
		this.amount = amount;
		this.month = month;
		this.day = day;
		this.year = year;
		this.description = description;
	}
	
	/**
	 * Constructor from SQL ResultSet
	 * @param results
	 * @throws SQLException
	 */
	public Expense(ResultSet results) throws SQLException{
		expenseID = results.getInt(1);
		amount = results.getDouble(2);
		month = results.getInt(3);
		day = results.getInt(4);
		year = results.getInt(5);
		description = results.getString(6);
	}
	
	/**
	 * Insert an Expense into the database
	 * @param connection
	 * @throws SQLException
	 */
	public void insert(Connection connection, FinancesDatabase fd) throws SQLException{
		PreparedStatement stmt = connection.prepareStatement(fd.getSql("Expense.insert"));
		stmt.setDouble(1, amount);
		stmt.setInt(2, year);
		stmt.setInt(3, month);
		stmt.setInt(4, day);
		stmt.setString(5, description);
		
		stmt.execute();
		stmt.close();
		expenseID = selectID(connection, fd);
	}
	/**
	 * Updates this Expense into database	
	 * @param connection
	 * @throws SQLException
	 */
	public void update(Connection connection, FinancesDatabase fd) throws SQLException{
		PreparedStatement stmt = connection.prepareStatement(fd.getSql("Expense.update"));
		stmt.setDouble(1, amount);
		stmt.setInt(2, year);
		stmt.setInt(3, month);
		stmt.setInt(4, day);
		stmt.setString(5, description);
		
		stmt.setInt(6, expenseID);
		
		stmt.execute();
		stmt.close();
	}
	
	/**
	 * Deletes this record from the database
	 * @param connection
	 * @throws SQLException
	 */
	public void delete(Connection connection, FinancesDatabase fd) throws SQLException{
		PreparedStatement stmt = connection.prepareStatement(fd.getSql("Expense.delete"));
		stmt.setInt(1, expenseID);
		
		stmt.execute();
		stmt.close();
	}
	/**
	 * Generates new Expense from a ResultSet
	 * @param connection
	 * @param id
	 * @return new Expense
	 * @throws SQLException
	 */
	public static Expense select(Connection connection, FinancesDatabase fd, int id) throws SQLException{
		PreparedStatement stmt = connection.prepareStatement(fd.getSql(""));
		stmt.setInt(1, id);
		ResultSet set = stmt.executeQuery();
		Expense exp = null;
		if (set.next()){
			exp = new Expense(set);
		}
		set.close();
		return exp;
	}
	
	/**
	 * Find the last id generated.
	 * @param connection
	 * @return The id
	 * @throws SQLException
	 */
	private int selectID(Connection connection, FinancesDatabase fd) throws SQLException{
		Statement stmt = connection.createStatement();
		ResultSet rs = stmt.executeQuery(fd.getSql("Expense.selectLastID"));
		int id=0;
		while (rs.next())
			id= rs.getInt(1);
		rs.close();
		stmt.close();
		return id;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Expense))
			return false;
		Expense other = (Expense) obj;
		if (expenseID != other.expenseID)
			return false;
		return true;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append(expenseID + "\t");
		sb.append(amount + "\t");
		sb.append(month + "/" + day + "/" + year + "\t");
		sb.append(description);
		
		
		return sb.toString();
	}
	
	public Vector<Object> toVector(){
		Vector<Object> v = new Vector<Object>();
		v.add(expenseID);
		v.add(amount);
		v.add(month + "/" + day + "/" + year + "\t");
		v.add(description);
		
		return v;
	}
	
	public static Vector<Vector<Object>> getExpenses(Connection connection, FinancesDatabase fd) throws SQLException{
		Statement stmt = connection.createStatement();
		ResultSet results = stmt.executeQuery(fd.getSql("Expense.getExpenses"));
		Vector<Vector<Object>> v = new Vector<Vector<Object>>();
		Vector<Object> exp;
		Expense e;
		
		while (results.next()){
			e = new Expense(results);
			exp = e.toVector();
			v.add(exp);
		}
		
		results.close();
		stmt.close();
		return v;
	}
	
	
}
